/**
 * @author yinyunqi
 * @datetime 2018年4月17日
 * @Content 
 */
package com.zcl.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zcl.service.RoleService;

@Controller
public class RoleController {
	@Autowired
	RoleService roleService;
	
	/**
	 * 权限管理页面
	 * @return
	 */
	@RequestMapping("/role")
	public String rolepage() {
		return "role";
	}
	
	/**
	 * 权限管理添加（用户）页面
	 * @return
	 */
	@RequestMapping("/roleAdd")
	public String roleAdd() {
		return "role_add";
	}
	
	/**
	 * 权限管理添加用户功能
	 */
	@ResponseBody
	@RequestMapping("/role/userAdd")
	public void roleUserAdd(HttpServletRequest request) {
		String userId = UUID.randomUUID().toString();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String roleLevel = request.getParameter("roleLevel");
		String other = request.getParameter("other");
		Map<String, String> map = new HashMap<String, String>();
		map.put("userId", userId);	//key - value
		map.put("userName", username);
		map.put("password", password);
		map.put("other", other);
		map.put("role_id", roleLevel);
		roleService.userAdd(map); 	//目的：添加用户
		roleService.user_role_Add(map);
	}
	
	/**
	 * 用户信息查询-全文搜索
	 * @param search_input
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/role/globalQuery")
	public List<Map<String, String>> accidentGlobalQuery(@RequestParam("search_input") String search_input){
		System.out.println(search_input);
		Map<String, String> map = new HashMap<String,String>();
		map.put("search_input", search_input);
		List<Map<String, String>> roleList = roleService.roleGlobalQuery(map);
		return roleList;
	} 
	
	/**
	 * 用户信息查询-全文搜索
	 * @param search_input
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/role/roleQuery")
	public List<Map<String, String>> roleRoleQuery(){
		Map<String, String> map = new HashMap<String,String>();
		List<Map<String, String>> roleList = roleService.roleQuery(map);
		return roleList;
	} 
	
	/**
	 * 用户信息查询-高级搜索
	 * @param search_input
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/role/advancedQuery")
	public List<Map<String, String>> roleAdvancedQuery(HttpServletRequest request){
		Map<String, String> map = new HashMap<String,String>();
		map.put("userId", request.getParameter("userId"));
		map.put("userName", request.getParameter("userName"));
		map.put("role", request.getParameter("role"));
		map.put("lastTime", request.getParameter("lastTime"));
		List<Map<String, String>> roleList = roleService.roleAdvancedQuery(map);
		return roleList;
	} 
	
	/**
	 * 用户信息概况-操作-编辑
	 * @return
	 */
	@RequestMapping("/roleSurveryRevise")
	public String accidentSuveryRevise(@RequestParam("userId") String userId,Model model) {
		Map<String, String> map = new HashMap<String,String>();
		map.put("userId", userId);
		Map<String, String> roleMap = roleService.roleSuveryInfo(map);
		model.addAttribute("user", roleMap);
		return "role_revise";
	}
	
	/**
	 * 用户信息概况-操作-编辑-获取下拉框的值
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/roleSurveryRevise2")
	public Map<String, String> accidentSuveryRevise2(@RequestParam("userId") String userId,Model model) {
		Map<String, String> map = new HashMap<String,String>();
		map.put("userId", userId);
		Map<String, String> roleMap = roleService.roleSuveryInfo2(map);
		return roleMap;
	}
	
	/**
	 * 权限管理修改用户功能
	 */
	@ResponseBody
	@RequestMapping("/role/reviseRole")
	public void roleReviseRole(HttpServletRequest request) {
		String username = request.getParameter("userName");
		String password = request.getParameter("password");
		String role = request.getParameter("role");
		String other = request.getParameter("other");
		Map<String, String> map = new HashMap<String, String>();
		map.put("userId", request.getParameter("userId"));	//key - value
		map.put("userName", username);
		map.put("password", password);
		map.put("other", other);
		map.put("role_id", role);
		roleService.roleReviseRole(map);
	}
	
	@RequestMapping("/role/delete")
	public void roleDelete(@RequestParam("userId") String userId) {
		Map<String, String> map = new HashMap<String,String>();
		map.put("userId", userId);
		roleService.roleDelete(map);
	}
}
